package com.example.onlinemonitoring.comCenter;

import com.example.onlinemonitoring.Util.CRC16;
import com.example.onlinemonitoring.Util.CommPortManager;
import com.example.onlinemonitoring.Util.HexUtil;
import com.example.onlinemonitoring.Util.IEEE754Utils;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Byqpdsensor;
import com.example.onlinemonitoring.serviceImpl.ByqpdsensorServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Scope;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service("byqpd61850Com")
@Scope("prototype")
public class Byqpd61850Com extends CommPortManager {
    @Autowired
    private ByqpdsensorServiceImpl byqpdserviceimpl;

    @Override
    @Transactional
    public void receiveData(String str) {
        setText("收到sanway请求：" + str + "\r\n");
        Date dt = new Date();
        if (str.length() == 10) {
            int startAddr = parseHex4(str.substring(2, 4));
            int addrLength = parseHex4(str.substring(4, 6));
            String s = "02";
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                Byqpdsensor byqpd = new Byqpdsensor();
                byqpd.setStationid(ad.getStationid());
                byqpd.setDevicegroup(ad.getDevicegroup());
                byqpd.setDeviceaddr(i);
                byqpd = byqpdserviceimpl.getModel(byqpd);
                if (byqpd == null) {
                    byqpd = new Byqpdsensor();
                }
                s += String.format("%02x", i);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                s += HexUtil.stringToAsciiTrim(sdf.format(dt));
                switch (isnull(byqpd.getLastdischargetype())) {
                    case "颗粒放电":
                        s += "00";
                        break;
                    case "间隙放电":
                        s += "01";
                        break;
                    case "电晕放电":
                        s += "02";
                        break;
                    case "电极放电":
                        s += "03";
                        break;
                    case "空穴放电":
                        s += "04";
                        break;
                    case "未知":
                        s += "05";
                        break;
                    default:
                        s += "00";
                        break;
                }
                s += String.format("%08X", isnull(byqpd.getLastrst()));
                s += String.format("%08X", isnull(byqpd.getLasttot()));
                s += IEEE754Utils.singleToHex(isnull(byqpd.getLastdischargeamount()));
                s += String.format("%08X", isnull(byqpd.getLastsignalpower()));
                if (isnull(byqpd.getLastcommunicationstatus()) == 0) {
                    s += String.format("%08X", 0);
                } else {
                    s += String.format("%08X", 1);
                }
            }
            s += CRC16.calcCrc16(s);
            Send(s);
        }
    }

    // @Async("threadPool")
    // public void startSendThread(Appdevice Appdevice) throws Exception {
    // checkType = "crc16";
    // issend.put(Thread.currentThread().getId(), true);
    // String cstr = "";
    // String cmdtype = "";
    // String nametype = "";
    // int timersp = (int) Math.round(Appdevice.getTimersp() * 1000);
    // while (issend.get(Thread.currentThread().getId()) != null) {
    // if (isrun) {
    // if (addrindex > Appdevice.getAddrmax() || addrindex < Appdevice.getAddrmin())
    // addrindex = Appdevice.getAddrmin();
    // cstr = String.format(Appdevice.getCmd(), String.format("%02X", addrindex));
    // cstr = cstr + CRC16.calcCrc16(cstr);
    // Send(cstr);
    // if (reSend[addrindex] > 3) {
    // byqpdserviceimpl.UpdateStatus(ad, addrindex, 1);
    // reSend[addrindex] = 0;
    // addrindex++;
    // }
    // reSend[addrindex]++;
    // } else if (rescode != 0) {
    // setText(resmsg[rescode]);
    // }
    // Thread.sleep(timersp);
    // }
    // }
}
